package com.buercorp.wangyu.core1.ch12.threads;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class TheradTest18 {

        public static void main(String[] args) {
        // 创建一个自定义的 ThreadFactory
        ThreadFactory threadFactory = new CustomThreadFactory();

        // 使用自定义的 ThreadFactory 创建线程池
        ExecutorService executor = Executors.newFixedThreadPool(10, threadFactory);

        // 提交任务到线程池中执行
        for (int i = 0; i < 10; i++) {
            executor.submit(new Task());
        }

        // 关闭线程池
        executor.shutdown();
    }

        // 自定义的 ThreadFactory
        static class CustomThreadFactory implements ThreadFactory {
            private int count = 0;

            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setName("CustomThread-" + count++);
                thread.setDaemon(true); // 设置为守护线程
                return thread;
            }
        }

        // 任务类
        static class Task implements Runnable {
            @Override
            public void run() {
                System.out.println("Task executed by " + Thread.currentThread().getName());
            }
        }
}
